/*
 * @Author: mjjh
 * @LastEditTime: 2023-04-16 21:00:02
 * @FilePath: \chagpt-shuowen\src\utils\request\axios.ts
 * @Description:
 */
import axios, { type AxiosResponse } from 'axios'
import {storeToRefs} from "pinia";
import {store} from "@/store";
import {useUserStore} from "@/store/modules/useUserStore";
const {tokenKey, authorization} = storeToRefs(useUserStore(store));
const service = axios.create({
  baseURL: import.meta.env.VITE_GLOB_API_URL,
})

service.interceptors.request.use(
  (config) => {
    const token = authorization?.value
    if (token)
      config.headers.Authorization = `Bearer ${token}`

    return config
  },
  (error) => {
    return Promise.reject(error.response)
  },
)

service.interceptors.response.use(
  (response: AxiosResponse): AxiosResponse => {
    if (response.status === 200)
      return response

    throw new Error(response.status.toString())
  },
  (error) => {
    return Promise.reject(error)
  },
)

export default service
